home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: const FAR* for a DLL
- Message-ID: <marnoldDpHwA9.Eq5@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <828873144.24425@uribe.demon.co.uk>
- Date: Sun, 7 Apr 1996 14:01:21 GMT
- Sender: marnold@netcom23.netcom.com
-
- roger@uribe.demon.co.uk (Roger Uribe) writes:
-
- >Below is an exctract from a DLL I am trying to write. The 2nd parameter
- >for FillRect is defined in windows.h as const RECT FAR* Could somebody
- >PLEASE tell me how to set it up - I've tried everything I can think of,
- >plus a few suggestions from others with no success so far.
- >e.g. &rect and other attempts to define a suitable pointer.
-
- >#include <windows.h>
- >//This is copied from windows.h
- >//int WINAPI FillRect(HDC, const RECT FAR*, HBRUSH);
-
- >int FAR PASCAL _export LibMain(HANDLE hInstance,WORD wDataSeg,
- > WORD wHeapSize,LPSTR lpszCmdLine)
- >{
- > if(wHeapSize > 0)
- > UnlockData(0);
- > return(1);
- >};
-
- >int FAR PASCAL _export myShow(int dest)
- >{
- >int i;
- >long colour;
- >typedef struct
- > { int left; int top; int right; int bottom;}
- > Srect;
-
- >Srect rect;
-
- Why are you using this Srect struct? Use RECT defined in "windows.h".
- This is the intended type for use with FillRect() (not to mention the
- rest of the Windows API).
-
- > colour = 0xFF00 ;
- > i= FillRect( dest, rect, CreateSolidBrush (colour)); <--- here please
- > return(i);
- >}
-
- >c:\...\drect.c(22) : error C2115: 'argument' : incompatible types
- >c:\...\drect.c(22) : warning C4024: 'FillRect' : different type
- > for formal and actual parameter 2
-
- You are getting these errors because you aren't using the right type,
- just as they suggest. FillRect() wants a pointer to a RECT, not your
- own custom Srect struct.
-
- You should be doing something like the following (taken from your above
- sample, but corrected to proper Windows types and perform necessary
- cleanup throughout)...
-
- #define STRICT
- #include "windows.h"
-
- int FAR PASCAL _export myShow(HDC dest)
- {
- int i;
- RECT rect;
- HBRUSH brush;
-
- // intialize rect to something!!!
-
- brush = CreateSolidBrush( RGB(0, 0xFF, 0 ));
- i = FillRect( dest, rect, brush );
- DeleteObject( brush );
- return i;
- }
-
- You're also not intializing rect to anything, which is a problem in and
- of itself. FillRect() expects the 2nd parameter to point to rectangle
- containing meaningful coordinates to fill.
-
- You're also not deleting the brush you create with CreateSolidBrush(),
- which leaks GDI resources every time you call myShow(), which if done
- often enough, will eventually lead to system failure.
-
- You're also passing CreateSolidBrush() a long. It takes a COLORREF.
- Your code will also be more readable if you intialize the colour using
- the RBG() macro defined in "windows.h", instead of a magic number like
- 0xFF00.
-
- One final note, you should also #define STRICT before including
- "windows.h". It allows your compiler to perform more type-checking on
- Windows types, making it more difficult to write code that uses those
- types incorrectly.
-
-
- Your main problem seems to be some kind of aversion to using the
- correct Windows types. Is there a reason to this?
-
- If you didn't realize you were using the wrong types, may I suggest
- a good Windows programming book. "Programming Windows" by Charles
- Petzold is the classic, and will expose you to these basic Windows
- programming details.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-